﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConsoleApp1;
namespace WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            panel2.Visible = false;
            textBox1.Text = Program.chosenOrder.ID;
            textBox2.Text = Program.chosenOrder.GetTime().ToString();
            textBox3.Text = Program.chosenOrder.GetCustomer().ToString();
            dataGridView1.DataSource = Program.chosenOrder.GetOrderDetails();
            dataGridView1.Refresh();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }


        private void button4_Click(object sender, EventArgs e)
        {
            InitializeComponent();
            Close();
            Form1 form1 = new Form1();
            form1.Visible=true;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;
            panel2.Visible = true;
            button5.Visible = true;
            button6.Visible = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Goods goods = new Goods(textBox4.Text, int.Parse(textBox5.Text));
            Program.chosenOrder.AddOrderDetails(new OrderDetail(goods, int.Parse(textBox6.Text)));
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = Program.chosenOrder.GetOrderDetails();
            dataGridView1.Refresh();
            dataGridView1.Visible = true;
            panel2.Visible = false;
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 1)
            {
                dataGridView1.Visible = false;
                panel2.Visible = true;
                button5.Visible = false;
                button6.Visible = true;
                textBox4.Text = Program.chosenOrder.GetOrderDetails()[dataGridView1.SelectedRows[0].Index].GoodsName;
                textBox5.Text = Program.chosenOrder.GetOrderDetails()[dataGridView1.SelectedRows[0].Index].GoodsPrice;
                textBox4.Refresh();
                textBox5.Refresh();
                textBox4.ReadOnly = true; textBox5.ReadOnly = true;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Program.chosenOrder.ModifyDetails(dataGridView1.SelectedRows[0].Index, int.Parse(textBox6.Text));
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = Program.chosenOrder.GetOrderDetails();
            dataGridView1.Refresh();
            dataGridView1.Visible = true;
            panel2.Visible = false;
            textBox4.ReadOnly = false; textBox5.ReadOnly = false;
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count==1) {
                Program.chosenOrder.RemoveDetails(dataGridView1.SelectedRows[0].Index);
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = Program.chosenOrder.GetOrderDetails(); 
            }
        }
    }
}
